<!DOCTYPE html>





<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 3.9.0">
  <link rel="apple-touch-icon" sizes="180x180" href="/blog/images/apple-touch-icon-next.png?v=7.4.0">
  <link rel="icon" type="image/png" sizes="32x32" href="/blog/images/favicon-32x32-next.png?v=7.4.0">
  <link rel="icon" type="image/png" sizes="16x16" href="/blog/images/favicon-16x16-next.png?v=7.4.0">
  <link rel="mask-icon" href="/blog/images/logo.svg?v=7.4.0" color="#222">

<link rel="stylesheet" href="/blog/css/main.css?v=7.4.0">


<link rel="stylesheet" href="/blog/lib/font-awesome/css/font-awesome.min.css?v=4.7.0">


<script id="hexo-configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/blog/',
    scheme: 'Muse',
    version: '7.4.0',
    exturl: false,
    sidebar: {"position":"left","display":"post","offset":12,"onmobile":false},
    copycode: {"enable":false,"show_result":false,"style":null},
    back2top: {"enable":true,"sidebar":false,"scrollpercent":false},
    bookmark: {"enable":false,"color":"#222","save":"auto"},
    fancybox: false,
    mediumzoom: false,
    lazyload: false,
    pangu: false,
    algolia: {
      appID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    },
    localsearch: {"enable":true,"trigger":"auto","top_n_per_article":5,"unescape":false,"preload":false},
    path: 'public/search.xml',
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    translation: {
      copy_button: '复制',
      copy_success: '复制成功',
      copy_failure: '复制失败'
    },
    sidebarPadding: 40
  };
</script>

  <meta name="description" content="本文使用uWsgi,supervisor,nginx来构建一个简单的python web程序，尽可能的模拟实际生产环境中的一些状况。  Flask应用新建一个简单的run.py 123456789101112from flask import Flask, escape, requestapp = Flask(__name__)@app.route(&apos;/&apos;)def hello():    nam">
<meta name="keywords" content="Python,nginx,Flask">
<meta property="og:type" content="article">
<meta property="og:title" content="Flask应用整合uWsgi,nginx,supervisor">
<meta property="og:url" content="http://xiejunhua.github.com/2019/09/09/test1/index.html">
<meta property="og:site_name" content="Junhua&#39;s Blog">
<meta property="og:description" content="本文使用uWsgi,supervisor,nginx来构建一个简单的python web程序，尽可能的模拟实际生产环境中的一些状况。  Flask应用新建一个简单的run.py 123456789101112from flask import Flask, escape, requestapp = Flask(__name__)@app.route(&apos;/&apos;)def hello():    nam">
<meta property="og:locale" content="zh-CN">
<meta property="og:image" content="https://tva1.sinaimg.cn/large/006y8mN6gy1g6tdcyfjhnj30s201imxg.jpg">
<meta property="og:updated_time" content="2019-09-09T08:58:35.907Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="Flask应用整合uWsgi,nginx,supervisor">
<meta name="twitter:description" content="本文使用uWsgi,supervisor,nginx来构建一个简单的python web程序，尽可能的模拟实际生产环境中的一些状况。  Flask应用新建一个简单的run.py 123456789101112from flask import Flask, escape, requestapp = Flask(__name__)@app.route(&apos;/&apos;)def hello():    nam">
<meta name="twitter:image" content="https://tva1.sinaimg.cn/large/006y8mN6gy1g6tdcyfjhnj30s201imxg.jpg">
  <link rel="canonical" href="http://xiejunhua.github.com/2019/09/09/test1/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome: false,
    isPost: true,
    isPage: false,
    isArchive: false
  };
</script>

  <title>Flask应用整合uWsgi,nginx,supervisor | Junhua's Blog</title>
  








  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .logo,
  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-CN">
  <div class="container use-motion">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-meta">

    <div>
      <a href="/blog/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">Junhua's Blog</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
        <p class="site-subtitle">日常工作学习记录，不定期更新</p>
      
  </div>

  <div class="site-nav-toggle">
    <button aria-label="切换导航栏">
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>


<nav class="site-nav">
  
  <ul id="menu" class="menu">
      
      
      
        
        <li class="menu-item menu-item-home">
      
    

    <a href="/blog/" rel="section"><i class="menu-item-icon fa fa-fw fa-home"></i> <br>首页</a>

  </li>
      
      
      
        
        <li class="menu-item menu-item-tags">
      
    

    <a href="/blog/tags/" rel="section"><i class="menu-item-icon fa fa-fw fa-tags"></i> <br>标签</a>

  </li>
      
      
      
        
        <li class="menu-item menu-item-categories">
      
    

    <a href="/blog/categories/" rel="section"><i class="menu-item-icon fa fa-fw fa-th"></i> <br>分类</a>

  </li>
      
      
      
        
        <li class="menu-item menu-item-archives">
      
    

    <a href="/blog/archives/" rel="section"><i class="menu-item-icon fa fa-fw fa-archive"></i> <br>归档</a>

  </li>
      <li class="menu-item menu-item-search">
        <a href="javascript:;" class="popup-trigger">
        
          <i class="menu-item-icon fa fa-search fa-fw"></i> <br>搜索</a>
      </li>
    
  </ul>

    

</nav>
  <div class="site-search">
    <div class="popup search-popup">
    <div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocorrect="off" autocapitalize="none"
           placeholder="搜索..." spellcheck="false"
           type="text" id="search-input">
  </div>
  <span class="popup-btn-close">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div id="search-result"></div>

</div>
<div class="search-pop-overlay"></div>

  </div>
</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>


    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
      <article itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block post">
    <link itemprop="mainEntityOfPage" href="http://xiejunhua.github.com/blog/2019/09/09/test1/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="Xie.Junhua">
      <meta itemprop="description" content="简单的描述测试">
      <meta itemprop="image" content="/blog/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Junhua's Blog">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">Flask应用整合uWsgi,nginx,supervisor

          
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              
                
              

              <time title="创建时间：2019-09-09 11:55:35 / 修改时间：16:58:35" itemprop="dateCreated datePublished" datetime="2019-09-09T11:55:35+08:00">2019-09-09</time>
            </span>
          
            

            
          
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing"><a href="/blog/categories/Python/" itemprop="url" rel="index"><span itemprop="name">Python</span></a></span>

                
                
              
            </span>
          

          
            <span class="post-meta-item" title="阅读次数">
              <span class="post-meta-item-icon">
                <i class="fa fa-eye"></i>
              </span>
              <span class="post-meta-item-text">阅读次数：</span>
              <span class="busuanzi-value" id="busuanzi_value_page_pv"></span>
            </span>
          

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <blockquote>
<p>本文使用uWsgi,supervisor,nginx来构建一个简单的python web程序，尽可能的模拟实际生产环境中的一些状况。</p>
</blockquote>
<h4 id="Flask应用"><a href="#Flask应用" class="headerlink" title="Flask应用"></a>Flask应用</h4><p>新建一个简单的run.py</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">from</span> flask <span class="keyword">import</span> Flask, escape, request</span><br><span class="line"></span><br><span class="line">app = Flask(__name__)</span><br><span class="line"></span><br><span class="line"><span class="meta">@app.route('/')</span></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">hello</span><span class="params">()</span>:</span></span><br><span class="line">    name = request.args.get(<span class="string">"name"</span>, <span class="string">"World"</span>)</span><br><span class="line">    <span class="keyword">return</span> <span class="string">f'Hello, <span class="subst">&#123;escape(name)&#125;</span>!'</span></span><br><span class="line"></span><br><span class="line"><span class="comment">#注意这里一定要将我们自己的启动方式放到main中，否则在uwsgi启动的时候会启动两个端口，uwsgi的socket的端口，和这里的8088端口</span></span><br><span class="line"><span class="keyword">if</span> __name__ == <span class="string">'__main__'</span>:</span><br><span class="line">    app.run(host=<span class="string">'0.0.0.0'</span>, port=<span class="number">8088</span>, debug=<span class="literal">True</span>)</span><br></pre></td></tr></table></figure>

<p>python文件写好了之后，直接执行python run.py</p>
<p>然后在浏览器中访问<code>{yourip}:8088</code></p>
<a id="more"></a>

<h4 id="uwsgi"><a href="#uwsgi" class="headerlink" title="uwsgi"></a>uwsgi</h4><p>uwsgi是一个web容器，使你的python web app更加健壮，性能更好。相当于tomcat之于java web。你自己写一个servlet的web服务虽然也能跑，但是性能肯定不好。</p>
<p>当然uwsgi的好处还有很多，用途也有很多，具体可以参考<a href="https://uwsgi-docs.readthedocs.io/en/latest/" target="_blank" rel="noopener">官网</a></p>
<p>安装uwsgi依赖<code>pip install uwsgi</code>，这里需要注意，如果你是用虚环境启动的web服务，那么需要将uwsgi安装到虚环境中，这里建议在线上或者测试环境部署的时候都使用虚环境，python3.5之后可以通过自带的命令建虚环境<code>python3 -m venv myenv</code>，然后就会帮你把虚环境建好，到<code>myenv/bin</code>下面执行你的Python或者pip操作。</p>
<h4 id="supervisor"><a href="#supervisor" class="headerlink" title="supervisor"></a>supervisor</h4><p>supervisor是一个进程控制系统，我们可以通过sueprvisor启动我们的后端服务，这样一旦后端服务挂掉了，supervisor就会再次将服务启动起来，不至于长时间影响线上的服务。这里我们使用的时候并没有开启web端，更多详细操作和配置可以在<a href="http://supervisord.org/" target="_blank" rel="noopener">官网</a>了解</p>
<h5 id="安装"><a href="#安装" class="headerlink" title="安装"></a>安装</h5><p><code>yum install supervisor</code></p>
<h5 id="配置supervisor"><a href="#配置supervisor" class="headerlink" title="配置supervisor"></a>配置supervisor</h5><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br></pre></td><td class="code"><pre><span class="line">; Sample supervisor config file.</span><br><span class="line"></span><br><span class="line">[unix_http_server]</span><br><span class="line">file=/var/run/supervisor/supervisor.sock   ; (the path to the socket file)</span><br><span class="line">;chmod=0700                 ; sockef file mode (default 0700)</span><br><span class="line">;chown=nobody:nogroup       ; socket file uid:gid owner</span><br><span class="line">;username=user              ; (default is no username (open server))</span><br><span class="line">;password=123               ; (default is no password (open server))</span><br><span class="line"></span><br><span class="line">;[inet_http_server]         ; inet (TCP) server disabled by default</span><br><span class="line">;port=127.0.0.1:9001        ; (ip_address:port specifier, *:port for all iface)</span><br><span class="line">;username=user              ; (default is no username (open server))</span><br><span class="line">;password=123               ; (default is no password (open server))</span><br><span class="line"></span><br><span class="line">[supervisord]</span><br><span class="line">logfile=/var/log/supervisor/supervisord.log  ; (main log file;default $CWD/supervisord.log)</span><br><span class="line">logfile_maxbytes=50MB       ; (max main logfile bytes b4 rotation;default 50MB)</span><br><span class="line">logfile_backups=10          ; (num of main logfile rotation backups;default 10)</span><br><span class="line">loglevel=info               ; (log level;default info; others: debug,warn,trace)</span><br><span class="line">pidfile=/var/run/supervisord.pid ; (supervisord pidfile;default supervisord.pid)</span><br><span class="line">nodaemon=false              ; (start in foreground if true;default false)</span><br><span class="line">minfds=1024                 ; (min. avail startup file descriptors;default 1024)</span><br><span class="line">minprocs=200                ; (min. avail process descriptors;default 200)</span><br><span class="line">;umask=022                  ; (process file creation umask;default 022)</span><br><span class="line">;user=chrism                 ; (default is current user, required if root)</span><br><span class="line">;identifier=supervisor       ; (supervisord identifier, default is &apos;supervisor&apos;)</span><br><span class="line">;directory=/tmp              ; (default is not to cd during start)</span><br><span class="line">;nocleanup=true              ; (don&apos;t clean up tempfiles at start;default false)</span><br><span class="line">;childlogdir=/tmp            ; (&apos;AUTO&apos; child log dir, default $TEMP)</span><br><span class="line">;environment=KEY=value       ; (key value pairs to add to environment)</span><br><span class="line">;strip_ansi=false            ; (strip ansi escape codes in logs; def. false)</span><br><span class="line"></span><br><span class="line">; the below section must remain in the config file for RPC</span><br><span class="line">; (supervisorctl/web interface) to work, additional interfaces may be</span><br><span class="line">; added by defining them in separate rpcinterface: sections</span><br><span class="line">[rpcinterface:supervisor]</span><br><span class="line">supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface</span><br><span class="line"></span><br><span class="line">[supervisorctl]</span><br><span class="line">serverurl=unix:///var/run/supervisor/supervisor.sock ; use a unix:// URL  for a unix socket</span><br><span class="line">;serverurl=http://127.0.0.1:9001 ; use an http:// url to specify an inet socket</span><br><span class="line">;username=chris              ; should be same as http_username if set</span><br><span class="line">;password=123                ; should be same as http_password if set</span><br><span class="line">;prompt=mysupervisor         ; cmd line prompt (default &quot;supervisor&quot;)</span><br><span class="line">;history_file=~/.sc_history  ; use readline history if available</span><br></pre></td></tr></table></figure>

<p>注意要注释掉<code>unix_http_server</code>中的<code>username</code>和<code>password</code>这些配置，否则在启动应用的时候会要求你配置用户名和密码。这里我们也注掉了不需要的url服务。</p>
<h5 id="添加应用配置"><a href="#添加应用配置" class="headerlink" title="添加应用配置"></a>添加应用配置</h5><p>安装完成之后，可以将我们的配置添加到<code>/etc/supervisord.d</code>目录中</p>
<p>新建文件example.ini</p>
<figure class="highlight ini"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="section">[program:example]</span></span><br><span class="line"><span class="attr">command</span>=/root/flask_web_app/bin/uwsgi --socket <span class="number">127.0</span>.<span class="number">0.1</span>:<span class="number">3032</span> --plugin python --wsgi-file run.py --callable app -H /root/flask_web_app</span><br><span class="line"></span><br><span class="line"><span class="attr">directory</span>=/root/flask_web_app/example_app</span><br><span class="line"><span class="attr">user</span>=root</span><br><span class="line"></span><br><span class="line"><span class="attr">autostart</span>=<span class="literal">true</span></span><br><span class="line"><span class="attr">autorestart</span>=<span class="literal">true</span></span><br><span class="line"><span class="attr">startretries</span>=<span class="number">1</span></span><br><span class="line"><span class="attr">stdout_logfile</span>=/root/logs/supervisor_out1.log</span><br><span class="line"><span class="attr">stderr_logfile</span>=/root/logs/supervisor_err1.log</span><br></pre></td></tr></table></figure>

<p>command是程序的启动命令，要注意，这里不要将启动命令放到一个nohup的脚本中，会导致supervisor无法监控他的状态，因为nohup启的是另外一个进程，而supervisor监控的是nohup的进程。</p>
<p>uwsgi命令参数说明：</p>
<p><code>/root/flask_web_app/bin/uwsgi</code> 是虚环境中uwsgi的路径</p>
<p><code>--socket 127.0.0.1:3032</code> 表示建立连接的端口</p>
<p><code>--wsgi-file run.py</code> 启动脚本的名字 </p>
<p><code>--callable app</code> 这里的app和你在run.py中定义的<code>app = Flask(__name__)</code>保持一致</p>
<p><code>-H /root/flask_web_app</code> 这里是指定虚环境的地址，这个很重要，一定要指定到虚环境的地址，否则在启动的时候是无法在你本机的python环境中找到你需要的一些依赖的</p>
<h5 id="启动服务"><a href="#启动服务" class="headerlink" title="启动服务"></a>启动服务</h5><p>应用配置完成之后，就可以启动我们的supervisor服务了</p>
<p><code>service supervisord start</code>默认会加载/etc/supervisor.conf的配置文件启动supervisor</p>
<p>启动完成之后，查看我们应用的状态</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">supervisorctl status all #查看状态</span><br><span class="line">supervisorctl start example</span><br><span class="line">supervisorctl restart example</span><br><span class="line">supervisorctl stop example</span><br><span class="line">supervisorctl update # 更新新的配置文件到supervisor</span><br><span class="line">supervisorctl reload # 重新启动配置中的所有程序</span><br></pre></td></tr></table></figure>

<p><img src="https://tva1.sinaimg.cn/large/006y8mN6gy1g6tdcyfjhnj30s201imxg.jpg" alt="image-20190906165605334"></p>
<p>running状态就表示我们的服务是正常启动的。</p>
<h4 id="nginx-配置"><a href="#nginx-配置" class="headerlink" title="nginx 配置"></a>nginx 配置</h4><h5 id="nginx-安装"><a href="#nginx-安装" class="headerlink" title="nginx 安装"></a>nginx 安装</h5><p><code>yum install nginx</code></p>
<h5 id="nginx配置"><a href="#nginx配置" class="headerlink" title="nginx配置"></a>nginx配置</h5><p>可以选择直接在<code>vi /etc/nginx/nginx.conf</code>修改，也可以在<code>/etc/nginx/nginx.d/</code>目录中添加配置文件</p>
<p>这里我们直接在<code>nginx.conf</code>文件中做修改</p>
<figure class="highlight nginx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="section">server</span> &#123;</span><br><span class="line">        <span class="attribute">listen</span>       <span class="number">80</span> default_server;</span><br><span class="line">        <span class="attribute">listen</span>       [::]:<span class="number">80</span> default_server;</span><br><span class="line">        <span class="attribute">server_name</span>  _;</span><br><span class="line">        <span class="attribute">root</span>         /usr/share/nginx/html;</span><br><span class="line"></span><br><span class="line">        <span class="comment"># Load configuration files for the default server block.</span></span><br><span class="line">        <span class="attribute">include</span> /etc/nginx/default.d/<span class="regexp">*.conf</span>;</span><br><span class="line"></span><br><span class="line">        <span class="attribute">location</span> / &#123;</span><br><span class="line">            <span class="attribute">include</span>  uwsgi_params;</span><br><span class="line">            <span class="attribute">uwsgi_pass</span>  <span class="number">127.0.0.1:3031</span>; </span><br><span class="line">        &#125;</span><br><span class="line">        <span class="attribute">error_page</span> <span class="number">404</span> /<span class="number">404</span>.html;</span><br><span class="line">            <span class="attribute">location</span> = /40x.html &#123;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="attribute">error_page</span> <span class="number">500</span> <span class="number">502</span> <span class="number">503</span> <span class="number">504</span> /50x.html;</span><br><span class="line">            <span class="attribute">location</span> = /50x.html &#123;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure>

<p>这里我们改动的位置只有<code>location /</code>里面的配置，注意这里的3031端口要和你启动的uwsgi的应用端口保持一致，如果服务器有多个web服务，可以添加多个server到nginx.conf中</p>
<h5 id="启动nginx"><a href="#启动nginx" class="headerlink" title="启动nginx"></a>启动nginx</h5><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">service nginx start</span><br></pre></td></tr></table></figure>

<p>现在我们直接在浏览器输入服务端的地址就能访问到我们的服务端了</p>

    </div>

    
    
    
        
      

      <footer class="post-footer">
          
            
          
          <div class="post-tags">
            
              <a href="/blog/tags/Python/" rel="tag"># Python</a>
            
              <a href="/blog/tags/nginx/" rel="tag"># nginx</a>
            
              <a href="/blog/tags/Flask/" rel="tag"># Flask</a>
            
          </div>
        

        

          <div class="post-nav">
            <div class="post-nav-next post-nav-item">
              
                <a href="/blog/2019/09/03/hello-world/" rel="next" title="Hello World">
                  <i class="fa fa-chevron-left"></i> Hello World
                </a>
              
            </div>

            <span class="post-nav-divider"></span>

            <div class="post-nav-prev post-nav-item">
              
                <a href="/blog/2019/09/13/zero-copy/" rel="prev" title="java中的零拷贝">
                  java中的零拷贝 <i class="fa fa-chevron-right"></i>
                </a>
              
            </div>
          </div>
        
      </footer>
    
  </div>
  
  
  
  </article>

  </div>


          </div>
          

        </div>
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">
        
        
        
        
      

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-4"><a class="nav-link" href="#Flask应用"><span class="nav-number">1.</span> <span class="nav-text">Flask应用</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#uwsgi"><span class="nav-number">2.</span> <span class="nav-text">uwsgi</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#supervisor"><span class="nav-number">3.</span> <span class="nav-text">supervisor</span></a><ol class="nav-child"><li class="nav-item nav-level-5"><a class="nav-link" href="#安装"><span class="nav-number">3.1.</span> <span class="nav-text">安装</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#配置supervisor"><span class="nav-number">3.2.</span> <span class="nav-text">配置supervisor</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#添加应用配置"><span class="nav-number">3.3.</span> <span class="nav-text">添加应用配置</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#启动服务"><span class="nav-number">3.4.</span> <span class="nav-text">启动服务</span></a></li></ol></li><li class="nav-item nav-level-4"><a class="nav-link" href="#nginx-配置"><span class="nav-number">4.</span> <span class="nav-text">nginx 配置</span></a><ol class="nav-child"><li class="nav-item nav-level-5"><a class="nav-link" href="#nginx-安装"><span class="nav-number">4.1.</span> <span class="nav-text">nginx 安装</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#nginx配置"><span class="nav-number">4.2.</span> <span class="nav-text">nginx配置</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#启动nginx"><span class="nav-number">4.3.</span> <span class="nav-text">启动nginx</span></a></li></ol></li></ol></div>
        
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
  <p class="site-author-name" itemprop="name">Xie.Junhua</p>
  <div class="site-description" itemprop="description">简单的描述测试</div>
</div>
  <nav class="site-state motion-element">
      <div class="site-state-item site-state-posts">
        
          <a href="/blog/archives/">
        
          <span class="site-state-item-count">5</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
    
      
      
      <div class="site-state-item site-state-categories">
        
          
            <a href="/blog/categories/">
          
        
        <span class="site-state-item-count">2</span>
        <span class="site-state-item-name">分类</span>
        </a>
      </div>
    
      
      
      <div class="site-state-item site-state-tags">
        
          
            <a href="/blog/tags/">
          
        
        <span class="site-state-item-count">6</span>
        <span class="site-state-item-name">标签</span>
        </a>
      </div>
    
  </nav>
  <div class="links-of-author motion-element">
      <span class="links-of-author-item">
      
      
        
      
      
        
      
        <a href="https://github.com/XieJunhua" title="GitHub &rarr; https://github.com/XieJunhua" rel="noopener" target="_blank"><i class="fa fa-fw fa-github"></i>GitHub</a>
      </span>
    
      <span class="links-of-author-item">
      
      
        
      
      
        
      
        <a href="mailto:xie.junhua@outlook.com" title="E-Mail &rarr; mailto:xie.junhua@outlook.com" rel="noopener" target="_blank"><i class="fa fa-fw fa-envelope"></i>E-Mail</a>
      </span>
    
  </div>



      </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2020</span>
  <span class="with-love" id="animate">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Xie.Junhua</span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io" class="theme-link" rel="noopener" target="_blank">Hexo</a> 强力驱动 v3.9.0</div>
  <span class="post-meta-divider">|</span>
  <div class="theme-info">主题 – <a href="https://theme-next.org" class="theme-link" rel="noopener" target="_blank">NexT.Muse</a> v7.4.0</div>

        
<div class="busuanzi-count">
  <script async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
    <span class="post-meta-item-icon">
      <i class="fa fa-user"></i>
    </span>
    <span class="site-uv" title="总访客量">
      <span class="busuanzi-value" id="busuanzi_value_site_uv"></span>
    </span>
  
    <span class="post-meta-divider">|</span>
  
    <span class="post-meta-item-icon">
      <i class="fa fa-eye"></i>
    </span>
    <span class="site-pv" title="总访问量">
      <span class="busuanzi-value" id="busuanzi_value_site_pv"></span>
    </span>
  
</div>












        
      </div>
    </footer>
  </div>

  


  <script src="/blog/lib/anime.min.js?v=3.1.0"></script>
  <script src="/blog/lib/velocity/velocity.min.js?v=1.2.1"></script>
  <script src="/blog/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
<script src="/blog/js/utils.js?v=7.4.0"></script><script src="/blog/js/motion.js?v=7.4.0"></script>
<script src="/blog/js/schemes/muse.js?v=7.4.0"></script>
<script src="/blog/js/next-boot.js?v=7.4.0"></script>



  








  <script src="/blog/js/local-search.js?v=7.4.0"></script>














  

  

  

</body>
</html>
